<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>微博找事</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/style.min.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/font-awesome.min93e3.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/bootstrap-switch.min.css"/>
<style type="text/css">
body{font-size: 14px;}
.panel-default>.panel-heading{background-color: white;}
.panel-footer{background-color: white;}
.panel-body{height: 160px;}
.panel-body:last-child{height: 200px;}
.peopleCount{color: rgb(26, 179, 148);text-decoration: underline;font-size: 20px;}
img{cursor: pointer;}
.bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-primary{background-color: rgb(26, 179, 148)}
</style>
</head>
<body class="gray-bg">
<div class="container-fluid">
	<div class="row" id="ruleList">
		<div class="col-sm-3">
			<div class="panel panel-default">
	            <div class="panel-heading">
	            	<span>微博找事</span>
	            </div>
	            <div class="panel-body text-center">
	            	<img onclick="func_baseSetDialog()" src="<%=path%>/images/tianJia.jpg" style="margin-top: 50px;">
	            </div>
	        </div>
		</div>
	</div>
</div>
<div style="display: none;">
	<div id="gtFive">
		<div class="row text-center">
			<p>您当前开启的任务已超过5个</p>
			<p>请关闭1个在开启</p>
		</div>
		<div class="row text-center" style="margin-top: 40px;">
			<button onclick="func_gtFiveHide()" class="btn btn-sm btn-primary" type="button">确定</button>
		</div>
	</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<%=path%>/hplus/js/bootstrap-switch.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
<script type="text/javascript">
var path = "<%=path%>";
var baseSetDialog = null;
var optionDialog = null;
var rule = null;
var gtFiveDialog = null;

$(function(){
	$(document).on('switchChange.bootstrapSwitch',':checkbox',function(event, state){
	  	var length = $(":checkbox:checked").length;
	  	if(length>5){
			gtFiveDialog = dialog({
				width: 400,
				height: 140,
				title: '提示',
				content: $("#gtFive").html()
			});
			gtFiveDialog.showModal();
			$(this).bootstrapSwitch('state', false);
			return ;
	  	}
		var text = "";
		if(state){
			text = "是否打开";
		}else{
			text = "是否关闭";
		}
		if(confirm(text)){
			var ruleId = $(this).val();
			$.post(path+"/scan/findPeople/openOrCloseRule",{ruleId:ruleId},function(data){
				if(data.msg_code == 0){
					$(this).bootstrapSwitch('state', !state, true);		
				}
			},"json");
		}else{
			$(this).bootstrapSwitch('state', !state, true);
		}
	});
	
	init();
});

function init(){
	func_getRuleList();
}

function func_Switchery(){
	var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
	elems.forEach(function(html) {
		var switchery = new Switchery(html,{
			color : "rgb(26, 179, 148)",
			size: 'small'
		});
	});
}

function func_getRuleList(){
	$("#ruleList>div:not(:last)").remove();
	$.post(path+"/scan/findThing/getRuleList",function(data){
		$.each(JSON.parse(data.data),function(i,a){
			var html = '';
			html += '<div class="col-sm-3">';
			html += '<div class="panel panel-default">';
			html += '<div class="panel-heading">';
			html += '<span>'+a.name+'</span>';
			html += '<div class="ibox-tools">';
			var isOpen = "";
			if(a.isOpen){
				isOpen = "checked";
			}
			html += '<input value="'+a.id+'" type="checkbox" class="js-switch" '+isOpen+'/>';
			html += '</div>';
			html += '</div>';
			html += '<div class="panel-body">';
			html += '<p style="margin-top:20px;" class="text-center">共找到事件：<a ruleId="'+a.id+'" href="'+path+'/view/member/findThing/list?ruleId='+a.id+'" class="peopleCount">'+a.thingCount+'</a>条</p>';
			html += '<p class="text-center">搜索频率：'+a.frequency+'</p>';
			html += '</div>';
			html += '<div class="panel-footer text-right">';
			html += '<img onclick="func_baseSetDialog('+a.id+')" src="'+path+'/images/browse_20.png" style="margin-right:16px"><img onclick="func_copy('+a.id+')" src="'+path+'/images/copy_20.png" style="margin-right:16px"><img onclick="func_delRule('+a.id+')" src="'+path+'/images/delete_20.png">';
			html += '</div>';
			html += '</div>';
			html += '</div>';
			
			$("#ruleList").prepend(html);
		});
		
		$(":checkbox").bootstrapSwitch({size: "mini"});
	},"json");
}

/**
 * 基础设置对话框
 */
function func_baseSetDialog(ruleId){
	if(ruleId != undefined){
		$.ajax({
			async: false,
			dataType:"json",
			url:path+"/scan/findPeople/getRule",
			data:{ruleId:ruleId},
			success: function(data){
				if(data.msg_code == 1){
					rule = JSON.parse(data.data);
				}
			}
		});
	}
	baseSetDialog = dialog({
		id: 'baseSetDialog',
		fixed: true,
		padding: 0,
		width: 800,
		height: 240,
		title:'基础设置',
		url:path+"/view/member/findPeople/baseSet",
		cancelDisplay: false,
		cancel: function () {
			rule = null;
			this.remove();
		}
	});
	baseSetDialog.showModal();
}

/**
 * 	发布者选项对话框
 */
function func_optionDialog(){
	var baseSetDialogBody = $("iframe[name='baseSetDialog']:visible").contents().find("body");
	
	if($.trim(baseSetDialogBody.find("#name").val()) == ""){
		alert("规则名称不能为空");
		return ;
	}
	
	var isNext = true;
	$.ajax({
		async: false,
		dataType:"json",
		url:path+"/scan/findPeople/countByName",
		data:{name:$.trim(baseSetDialogBody.find("#name").val()), type: 2},
		success: function(data){
			if(data.msg_code == 1){
				if(data.data != 0){
					alert("名称已存在");
					isNext = false;
				}
			}
		}
	});
	
	if(!isNext){
		return ;
	}
	
	var frequencyObj = baseSetDialogBody.find(":radio[name='frequency']:checked");
	if($.trim(frequencyObj.parent().next().val()) == ""){
		alert("搜索频率不能为空");
		return ;
	}
	
	optionDialog = dialog({
		id: 'optionDialog',
		fixed: true,
		padding: 0,
		width: 800,
		height: 450,
		title:'发布者选项',
		url:path+"/view/member/findThing/option",
		cancelDisplay: false,
		cancel: function () {
			if(!baseSetDialog.open){
				rule = null;
			}
			this.remove();
		}
	});
	optionDialog.showModal();
	$("iframe[name='optionDialog']:visible").attr("scrolling","auto").css("overflow-x","hidden").css("overflow-y","auto");
}

function saveDialog(){
	var optionDialogBody = $("iframe[name='optionDialog']:visible").contents().find("body");
	var keyword = optionDialogBody.find("#keyword").val();
	var keyword2 = optionDialogBody.find("#keyword2").val();
	
	if($.trim(keyword) == ""){
		alert("主关键词不能为空");
		return ;
	}
	if($.trim(keyword2) == ""){
		alert("组合关键词不能为空");
		return ;
	}
	
	if($.trim(keyword).split(",").length>5){
		alert("主关键词不能超过5个");
		return ;
	}
	if($.trim(keyword2).split(",").length>5){
		alert("组合关键词不能超过5个");
		return ;
	}
	
	var json = func_dealParam();
	
	if(rule != null){
		if(rule.isOpened == 1){
			alert("已经启动过，不允许修改");
			return ;
		}
	}
	
	$.post(path+"/scan/findThing/saveRule",json,function(data){
		if(data.msg_code == 1){
			optionDialog.remove();
			baseSetDialog.remove();
			init();
		}
	},"json");
}

/**
 * 	处理参数
 */
function func_dealParam(){
	var json = {};
	
	var baseSetDialogBody = $("iframe[name='baseSetDialog']:visible").contents().find("body");
	json.name = baseSetDialogBody.find("#name").val();
	var frequencyObj = baseSetDialogBody.find(":radio[name='frequency']:checked");
	json.frequency = frequencyObj.parent().next().val() + frequencyObj.val() + "/次";
	var starttimeObj = baseSetDialogBody.find(":radio[name='starttime']:checked");
	json.starttime = starttimeObj.parent().next().val();
	
	var optionDialogBody = $("iframe[name='optionDialog']:visible").contents().find("body");
	var keyword = optionDialogBody.find("#keyword").val();
	var keyword2 = optionDialogBody.find("#keyword2").val();
	var newKeyword = "";
	
	$.each(keyword.split(","),function(i,d){
		if(d != ""){
			$.each(keyword2.split(","),function(i2,d2){
				if(d2 != ""){
					newKeyword += ",("+d+" "+d2+")";
				}
			});	
		}
	});
	
	if(newKeyword != ""){
		newKeyword = newKeyword.substr(1);
	}
	json.keyword = newKeyword;
	
	var hasv = optionDialogBody.find(":radio[name='hasv']:checked").val();
	var hasori = optionDialogBody.find(":radio[name='hasori']:checked").val();
	var hasret = optionDialogBody.find(":radio[name='hasret']:checked").val();
	var hastext = optionDialogBody.find(":radio[name='hastext']:checked").val();
	var haspic = optionDialogBody.find(":radio[name='haspic']:checked").val();
	var hasvideo = optionDialogBody.find(":radio[name='hasvideo']:checked").val();
	var hasmusic = optionDialogBody.find(":radio[name='hasmusic']:checked").val();
	var haslink = optionDialogBody.find(":radio[name='haslink']:checked").val();
	var hasat = optionDialogBody.find(":radio[name='hasat']:checked").val();
	var province = optionDialogBody.find("#province").val();
	var city = optionDialogBody.find("#city").val();
	
	if(rule != null){
		json.id = rule.id
	}
	
	if(hasv != ""){
		json.hasv = hasv;
	}
	if(hasori != ""){
		json.hasori = hasori;
	}
	if(hasret != ""){
		json.hasret = hasret;
	}
	if(hastext != ""){
		json.hastext = hastext;
	}
	if(haspic != ""){
		json.haspic = haspic;
	}
	if(hasvideo != ""){
		json.hasvideo = hasvideo;
	}
	if(hasmusic != ""){
		json.hasmusic = hasmusic;
	}
	if(haslink != ""){
		json.haslink = haslink;
	}
	if(hasat != ""){
		json.hasat = hasat;
	}
	if(province != ""){
		json.province = province;
	}
	if(city != ""){
		json.city = city;
	}
	return json;
}

/**
 * 	删除规则
 */
function func_delRule(ruleId){
	if(confirm("是否删除")){
		$.post(path+"/scan/findPeople/delRule",{ruleId:ruleId},function(data){
			init();
		});
	}
}

/**
 * 	复制
 */
function func_copy(ruleId){
	if(confirm("是否复制")){
		$.post(path+"/scan/findPeople/copyRule",{ruleId:ruleId},function(data){
			init();
		});
	}
}

function func_gtFiveHide(){
	gtFiveDialog.remove();
}
</script>
</html>